<?php
namespace app\common\model\jooj_common;
use think\Model;
class Regions extends Model
{

    protected $connection = ['database' => 'jooj_common'];
    protected $table = 'jj_regions';
    
    public function _initialize() {
        parent::_initialize();
    }
    
    
    /**
     * 获取全部地域
     * @param string $dataType 返回数据类型：basic 基本信息（包含id和name），all 全部信息
     */
    public function getAllRegions($dataType = 'basic') {
        if ($dataType == 'basic') {
            $field = 'region_id,region_name';
        } elseif ($dataType == 'all') {
            $field = '*';
        }
        return $this->field($field)->select();
    }

    /**
     * 根据省市区查询相应编码（市级查询必须精确到"市"字，否则查询不出相应编码）
     * @param type $province
     * @param type $city
     * @param type $district
     */
    public function getOneByLocation($province, $city, $district) {
        $data = ["province_id" => "", "city_id" => "", "district_id" => ""];
        $province = trim($province);
        $city = trim($city);
        $district = trim($district);
        if (empty($province))
            return false;
        //省份补齐
        if (strpos($province, "上海") !== false || strpos($province, "北京") !== false ||
            strpos($province, "天津") !== false || strpos($province, "重庆") !== false) {
            if (strpos($province, "市") === false)
                $province .= "市";
            if (strpos($city, "县") === false) 
                $city = "市辖区";
        } else {
            if (strpos($province, "省") === false)
                $province .= "省";
        }

        $prWhere = ["region_name" => $province,
            "region_type" => 1];
        $provinceRs = $this->where($prWhere)->find();
        if (!empty($provinceRs)) {
            $data["province_id"] = $provinceRs['region_id'];
            if (!empty($city)) {
                $ciWhere = ["parent_id"   => $provinceRs['region_id'],
                            "region_name" => $city,
                            "region_type" => 2];
                $cityRs = $this->where($ciWhere)->find();
                if (!empty($cityRs)) {
                    $data["city_id"] = $cityRs['region_id'];
                    if (!empty($district)) {
                        $diWhere = ["parent_id"   => $cityRs['region_id'],
                                    "region_name" => $district,
                                    "region_type" => 3];
                        $districtRs = $this->where($diWhere)->find();
                        if (!empty($districtRs)) {
                            $data["district_id"] = $districtRs['region_id'];
                        }
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 获取全部地域
     * 区域id => 区域名称
     * 调用缓存名 regionsName
     */
    public function getRegions() {
       
        return $this->cache('regionsName')->getField('region_id, region_name');
    }
    
    /**
     * 获取市级城市信息
     */
    public function get_city() { 
        return $this->field('region_name,region_id,first_letter,lng,lat')
                    ->where(['region_type'=>2,'first_letter'=>['exp','is not null'],'first_letter'=>['neq','']])
                    ->order('convert(first_letter using gbk)')
                    ->cache(true)
                    ->select();
    }
    
    /**
     * 获取市级城市信息
     */
    public function get_city_id_by_city_name($city_name) { 
        return $this->where(['region_name'=>$city_name])
                    ->value('region_id');
    }
}